package com.ruoyi.tools;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.ruoyi.common.utils.StringUtils;

public class PublishMsg {

	// 创建固定⼤⼩为100 的线程池
	private static ExecutorService service = Executors.newFixedThreadPool(1000);

	// 发送消息的业务逻辑⽅法
	public void sendMsg(List<String> openids, Map map,String tplid,String page) {
		long begin = System.nanoTime();
		// 循环发送消息
		for (int i = 0; i < openids.size(); i++) {
			String openid = openids.get(i);
			// 使⽤Runnable
			service.submit(new Runnable() {
				@Override
				public void run() {
					// 调⽤相对⽐较耗时的发送消息接⼝
					try {
						Thread.sleep(200);
						//发送模板消息
						sendMsg(openid, map,tplid, page);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}
		System.out.println("发送消息结束，耗时：" + (System.nanoTime() - begin) / 1000_000d);
	}


	// 完成发送消息
	private void sendMsg(String openid, Map map,String tplid,String page) {
		
		//发送模板消息
				String access_token = WxTool.getAccessToken();
						
				SubscribeMessage subscribeMessage = new SubscribeMessage();
				//access_token
				subscribeMessage.setAccess_token(access_token);
				//openId
				subscribeMessage.setTouser(openid);
				//模板消息ID
				subscribeMessage.setTemplate_id(tplid);
				//跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
				subscribeMessage.setMiniprogram_state("trial");
				//模板内容
				subscribeMessage.setData(map);
				//跳转小程序页面
				if(StringUtils.isNotEmpty(page)) {
					subscribeMessage.setPage(page);
				}
				          
				//发送
				WxTool.serndMsg(subscribeMessage);
		
	}

}
